package com.sphd.beautysalon.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.Button;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.CardInfo;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.AddEticketPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.AddEticketView;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddEticketActivity extends BaseActivity implements AddEticketView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.add_eticket_givelimit)
    MaterialEditText addEticketGivelimit;
    @BindView(R.id.add_eticket_value)
    MaterialEditText addEticketValue;
    @BindView(R.id.add_eticket_upperlimit)
    MaterialEditText addEticketUpperlimit;
    @BindView(R.id.add_eticket_limitdays)
    MaterialEditText addEticketLimitdays;
    @BindView(R.id.addetickets_update_btn)
    Button addeticketsUpdateBtn;
    @BindView(R.id.content_add_eticket)
    NestedScrollView contentAddEticket;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    CardInfo cardInfo;
    private String givelimit, upperlimit, value, limitdays;// 优惠卷有效期剩余的天数
    LoginInfo loginInfo;
    AddEticketPresenterImpl addEticketPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_eticket);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        cardInfo = (CardInfo) bundle.getSerializable(Constant.CARDINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        addEticketPresenter = new AddEticketPresenterImpl(this);
        setVal();
    }

    private void setVal() {
        if (cardInfo == null) {
            setTitleBar(toolbar, "新增优惠券");
            addEticketUpperlimit.setEnabled(true);
        } else {
            setTitleBar(toolbar, "修改优惠券");
            addEticketGivelimit.setText(cardInfo.getCard_givelimit() + "");
            addEticketUpperlimit.setText(cardInfo.getCard_upperlimit() + "");
            addEticketValue.setText(cardInfo.getCard_value() + "");
            addEticketLimitdays.setText(cardInfo.getCard_limitdays() + "");
            addEticketUpperlimit.setEnabled(false);
        }

    }

    @OnClick(R.id.addetickets_update_btn)
    public void onClick() {
        if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
            toast("店长会去操作，您不用费心了");
        } else {
            if (getMessage()) {
                if (cardInfo == null) {
                    addEticketPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.AddEtickets + "givelimit=" + givelimit + "&givenumber=1&upperlimit=" + upperlimit +
                            "&value=" + value + "&limitdays=" + limitdays + "&cgid=" + restaurantInfo.getId());
                } else {
                    addEticketPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ChangeEtickets + "givelimit=" + givelimit + "&givenumber=1&upperlimit=" + upperlimit +
                            "&value=" + value + "&limitdays=" + limitdays + "&cardid=" + cardInfo.getId());
                }


            }
        }

    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("提交成功");
            setResult(1);
            finish();
        } else {
            toast("提交失败，请重试。");
        }
    }

    private boolean getMessage() {
        boolean isempty = false;
        if (!TextUtils.isEmpty(addEticketGivelimit.getText())) {
            givelimit = addEticketGivelimit.getText().toString().trim();//
            isempty = true;
        } else {
            toast("获取条件为空");
            isempty = false;
            return isempty;
        }
        if (!TextUtils.isEmpty(addEticketUpperlimit.getText())) {
            upperlimit = addEticketUpperlimit.getText().toString().trim();//
            isempty = true;
        } else {
            toast("使用条件为空");
            isempty = false;
            return isempty;
        }
        if (!TextUtils.isEmpty(addEticketValue.getText())) {
            value = addEticketValue.getText().toString().trim();//
            isempty = true;
        } else {

            toast("优惠券金额为空");
            isempty = false;
            return isempty;
        }
        if (!TextUtils.isEmpty(addEticketLimitdays.getText())) {
            limitdays = addEticketLimitdays.getText().toString().trim();//
            isempty = true;
        } else {
            toast("有效期天数为空");
            isempty = false;
            return isempty;
        }
        return isempty;
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
